package sculove.spring.framework.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sculove.spring.zzz.attachFile.model.AttachFileDTO;


/**
 * @author sculove
 * @deprecated
 */
public class AttachFileUtil {
    @SuppressWarnings("unused")
    private static Log log = LogFactory.getLog("AttachFileUtil");
    
    public static List<AttachFileDTO> createAttachFileList(HttpServletRequest request) throws IOException {
    	if(!ServletFileUpload.isMultipartContent(request)) return null;
    	
        XMLConfiguration conf = Configuration.getXMLConfiguration();
        List<AttachFileDTO> attachFileList = new ArrayList<AttachFileDTO>();
        String filePath = "";
        String fileName = "";
        StringBuffer sb = new StringBuffer();

		ServletFileUpload upload = new ServletFileUpload();
		FileItemIterator iter;
		try {
			iter = upload.getItemIterator(request);
			byte buffer[] = new byte[4096];
			BufferedInputStream bis = null;
			FileOutputStream out = null;
			
			while(iter.hasNext()) {
				FileItemStream item = iter.next();
				// File�� ���.
				if(!item.isFormField()) {
					try {
						bis = new BufferedInputStream(item.openStream());
						out = new FileOutputStream("Ddd");
						int length = 0;
						while ((length = bis.read(buffer)) > -1) {
							out.write(buffer, 0, length);
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						try {if(bis !=null) { bis.close(); bis =null; }} catch (Exception e_bis) {}
						try {if(out !=null) { out.close(); out =null; }} catch (Exception e_out) {}						
					}
				}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        
        
        
        
        for(int i=0; i<multipartFileList.size(); i++) {
            if( multipartFileList.get(i)!=null &&
                multipartFileList.get(i).getOriginalFilename()!=null &&
                !"".equals(multipartFileList.get(i).getOriginalFilename()) ) {
                
                // �⵵,�� ����� ��
                filePath = sb.append(conf.getString("fileData.path"))
                            .append(conf.getString("com.separator"))
                            .append(DateUtil.getYear())
                            .append(conf.getString("com.separator"))
                            .append(DateUtil.getMonth())
                            .append(conf.getString("com.separator")).toString();
                sb.delete(0, sb.length());
                
                // ���� �տ� union key ��
                fileName = ComUtil.getUniqueNo() + "_" + multipartFileList.get(i).getOriginalFilename();
                
                // �ӽ�������� ����.
                multipartFileList.get(i).transferTo(new File(filePath + fileName));
                
                AttachFileDTO attachFile = new AttachFileDTO();
                attachFile.setLgcFilename(multipartFileList.get(i).getOriginalFilename());
                attachFile.setFilePath(filePath);
                attachFile.setPhyFilename(fileName);
                attachFile.setFileSeq((short)0);
                attachFile.setFileSize(multipartFileList.get(i).getSize());
                
                attachFileList.add(attachFile);
            }
        }
        return attachFileList;
    }
    
    public static void deleteAttachFileList(List<AttachFileDTO> attachFileList) {
        File file = null;
        
        for(int i=0; i<attachFileList.size(); i++) {
            file = new File(attachFileList.get(i).getFilePath() + attachFileList.get(i).getPhyFilename());
            file.delete();
        }
    }  
}
